package com.radar.io;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.radar.common.CinradType;
import com.radar.common.FileType;
import com.radar.exception.RadarDecodeError;
import com.radar.io.cinrad.*;
import com.radar.io.standard.RadarStandard;

import java.io.File;

/**
 * @author: fy-wbj
 * @description: cinrad radar
 * @date: 2020/9/17
 */
public class CinradRadar extends BaseRadr {

    static Log log = LogFactory.get();

    public BaseCinrad radar;

    String ncFile = null;

    @Override
    public void parse(String file) throws RadarDecodeError {
        this.parse(file, null, null);
    }

    public void parse(String file, CinradType type) throws RadarDecodeError {
        this.parse(file, type, null);
    }

    @Override
    public void dispose() {
        radar.dispose();
    }

    public void parse(String file, CinradType type, String code) throws RadarDecodeError {
        super.parse(file);
        inferType(file);
        if (type != null) {
            if (this.type != type) {
                log.warn("Contradictory information from input radar type and radar type detected from input file.");
            }
            this.type = type;
        } else {
            if (this.type == null) {
                log.error("Unable to determine the file type. Use `radar_type` keyword to specify the radar type.");
                throw new RadarDecodeError();
            }
        }
        if (code != null) {
            this.code = code;
        }
        try {
            // 先解析 standard 雷达 判断是否是新一代天气雷达
            boolean standardRadar = RadarStandard.isStandardRadar(this.bytes);
            if (standardRadar) {
                this.datType = FileType.STANDARD;
                this.radar = new RadarStandard(file, this.type, this.code);
                this.radar.setNetcdfFile(ncFile);
                this.radar.parse(this.bytes);
                return;
            }
            if (this.type == CinradType.SA || this.type == CinradType.SB || this.type == CinradType.SAD) {
                this.radar = new Radar88D(file, this.type, this.code);
                this.radar.setNetcdfFile(ncFile);
                this.radar.parse(this.bytes);
            } else if (this.type == CinradType.CA || this.type == CinradType.CB) {
                this.radar = new RadarCAB(file, this.type, this.code);
                this.radar.setNetcdfFile(ncFile);
                this.radar.parse(this.bytes);
            } else if (this.type == CinradType.SC || this.type == CinradType.CD) {
                this.radar = new RadarSC(file, this.type, this.code);
                this.radar.setNetcdfFile(ncFile);
                this.radar.parse(this.bytes);
            } else if (this.type == CinradType.CC) {
                this.radar = new RadarCC(file, this.type, this.code);
                this.radar.setNetcdfFile(ncFile);
                this.radar.parse(this.bytes);
            } else if (this.type == CinradType.RADAR_382) {
                this.radar = new Radar382(file, this.type, this.code);
                this.radar.setNetcdfFile(ncFile);
                this.radar.parse(this.bytes);
            } else if (this.type == CinradType.RADAR_720) {
                this.radar = new Radar720(file, this.type, this.code);
                this.radar.setNetcdfFile(ncFile);
                this.radar.parse(this.bytes);
            } else if (this.type == CinradType.RADAR_06CDN) {
                this.radar = new Radar06CDN(file, this.type, this.code);
                this.radar.setNetcdfFile(ncFile);
                this.radar.parse(this.bytes);
            } else if (this.type == CinradType.RADAR_721) {
                this.radar = new Radar721(file, this.type, this.code);
                this.radar.setNetcdfFile(ncFile);
                this.radar.parse(this.bytes);
            } else if (this.type == CinradType.SC784) {
                this.radar = new Radar784(file, this.type, this.code);
                this.radar.setNetcdfFile(ncFile);
                this.radar.parse(this.bytes);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("尝试解析sc 雷达");
            this.radar = new RadarSpecial(file, this.type, this.code);
            this.radar.parse(this.bytes);
        }
    }

    public void setNetcdfFile(String file) {
        this.ncFile = file;
    }
    public void inferType(String filePath) throws RadarDecodeError {
        File f = new File(filePath);
        try {
            String file = f.getName();
            byte[] s = new byte[9];
            System.arraycopy(this.bytes, 100, s, 0, 9);
            String types = new String(s);
            if (types.equals("CINRAD/SC")) {
                this.type = CinradType.SC;
            } else if (types.equals("CINRAD/CD")) {
                this.type = CinradType.CD;
            }
            System.arraycopy(this.bytes, 116, s, 0, 9);
            types = new String(s);
            if (types.equals("CINRAD/CC")) {
                this.type = CinradType.SC;
            }
            // Read information from filename (if applicable)
            String[] spart;
            if (file.startsWith("RADA")) {
                spart = file.split("-");
                if (spart.length > 2) {
                    this.code = spart[1];
                    this.type = CinradType.valueOf(spart[2]);
                }
            } else if (file.startsWith("Z")) {
                spart = file.split("_");
                if (spart.length > 2) {
                    this.code = spart[3];
                    if(spart[7].equals("CDD")){
                        spart[7] = "CD";
                    }
                    this.type = CinradType.valueOf(spart[7]);
                }
            }
        } catch (Exception e) {
//            log.error("find radar type faild");
            throw new RadarDecodeError("find radar type faild");
        }
    }

    public void setCode(String code) {
        this.code = code;
    }

}
